package com.kotlin.android.ktx.ext

import android.content.Context.INPUT_METHOD_SERVICE
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.kotlin.android.ktx.ext.ViewExt.getColor

/**
 * 创建者: zl
 * 创建时间: 2020/6/8 10:35 AM
 * 描述:View事件扩展
 */

fun View.getDrawable(@DrawableRes id: Int?): Drawable? = id?.let { context?.getDrawable(it) }

object ViewExt {
    /**
    扩展点击事件
     */
    fun View.onClick(listener: View.OnClickListener): View {
        setOnClickListener(listener)
        return this
    }

    /**
    扩展点击事件，参数为方法
     */
    fun View.onClick(method: () -> Unit): View {
        setOnClickListener { method() }
        return this
    }

    fun View.getString(@StringRes resId: Int, vararg params: Any): String {
        return context.getString(resId, *params)
    }

    fun View.getColor(@ColorRes resId: Int): Int {
        return ContextCompat.getColor(context, resId)
    }

    fun View?.showMessage(message: String, length: Int = Toast.LENGTH_SHORT) {
        //todo 根据经验，这里多半需要自定义toast的样式
        this?.apply {
            Toast.makeText(context, message, length).show()
        }
    }

    /**
     * 弹出软键盘
     */
    fun View.showSoftInput() {
        requestFocus()
        val imm = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
    }

    /**
     * 隐藏软键盘
     */
    fun View.hideSoftInput() {
        val imm = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(windowToken, 0)
    }

    fun View.visible() {
        this.visibility = View.VISIBLE
    }

    fun View.gone() {
        this.visibility = View.GONE
    }

    fun View.invisible() {
        this.visibility = View.INVISIBLE
    }
}

/**
 * View设置选择器，指定圆角 [cornerRadius]（dp），边框 [strokeWidth]
 */
fun View.setSelector(
    @ColorRes pressColor: Int,
    @ColorRes pressStrokeColor: Int,
    @ColorRes normalColor: Int,
    @ColorRes normalStrokeColor: Int,
    cornerRadius: Int,
    strokeWidth: Int = 1
): View {
    val color = getColorStateList(getColor(normalColor), pressColor = getColor(pressColor))
    val strokeColor = getColorStateList(getColor(normalStrokeColor), pressColor = getColor(pressStrokeColor))
    val gd = GradientDrawable()
    gd.cornerRadius = dp2pxF(cornerRadius)
    gd.color = color
    gd.setStroke(strokeWidth, strokeColor)
    background = gd

    return this
}

/**
 * 设置渐变色背景，指定圆角 [cornerRadius]（dp）
 */
fun View.setGradientColor(
    @ColorRes startColor: Int,
    @ColorRes endColor: Int,
    cornerRadius: Int,
    orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TOP_BOTTOM
): View {
    val gd = GradientDrawable(orientation, intArrayOf(getColor(startColor), getColor(endColor)))
    gd.cornerRadius = dp2pxF(cornerRadius)
    background = gd

    return this
}